package theone.ui;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;

public class UiPanel extends UiComponent {
	
	private Image[] m_images;
	private Image m_background;
	
	public UiPanel(Image background, Image[] images)
	{
		m_background = background;
		m_images = images;
	}

	/**
	 * ---------------------
	 * | 1 |     5     | 4 |
	 * ---------------------
	 * | 6 |     *     | 8 |
	 * ---------------------
	 * | 2 |     7     | 3 |
	 * ---------------------  * = background
	 */
	public void draw(Graphics g, int offsetX, int offsetY) {
		
		// Manage hover
		Color currentColor = Faded;
		if (isHover())
		{
			currentColor = Bright;
		}
		
		// Draw background
		{
			int x_rat = getWidth() / m_background.getWidth();
			int y_rat = getHeight() / m_background.getWidth();
			int x_plus = getWidth() % m_background.getWidth();
			int y_plus = getHeight() % m_background.getWidth();
			for (int i = 0; i < x_rat; i++)
				for (int j=0; j < y_rat; j++)
					g.drawImage(m_background, offsetX + getX() + i*m_background.getWidth(),
						offsetY + getY() + j*m_background.getHeight(), currentColor);
			
			if (x_plus > 0) {
				for (int j=0; j < y_rat; j++)
					g.drawImage(m_background.getSubImage(0, 0, x_plus, m_background.getHeight()), 
						offsetX + getX() + x_rat*m_background.getWidth(),
						offsetY + getY() + j*m_background.getHeight(), currentColor);
			}
			
			if (x_plus > 0 && y_plus > 0)
				g.drawImage(m_background.getSubImage(0, 0, x_plus, y_plus),
						offsetX + getX() + x_rat*m_background.getWidth(),
						offsetY + getY() + y_rat*m_background.getHeight(), currentColor);
			
			if (y_plus > 0) {
			for (int i=0; i < x_rat; i++)
				g.drawImage(m_background.getSubImage(0, 0, m_background.getWidth(), y_plus), 
						offsetX + getX() + i*m_background.getWidth(),
						offsetY + getY() + y_rat*m_background.getHeight(), currentColor);
			}
		
		}
		
		{
			// Upper border
			int x_rat = (getWidth() - (m_images[0].getWidth()/2) - (m_images[3].getWidth()/2)) / m_images[4].getWidth();
			int x_plus = (getWidth() - (m_images[0].getWidth()/2) - (m_images[3].getWidth()/2)) % m_images[4].getWidth();
			for (int i = 0; i < x_rat; i++)
				g.drawImage(m_images[4], 
					offsetX + getX() + i*m_images[4].getWidth() + (m_images[0].getWidth()/2),
					offsetY + getY() - (m_images[4].getHeight()/2), currentColor);
			if (x_plus > 0)
				g.drawImage(m_images[4].getSubImage(0, 0, x_plus, m_images[4].getHeight()),
					offsetX + getX() + x_rat*m_images[4].getWidth() + (m_images[0].getWidth()/2),
					offsetY + getY() - (m_images[4].getHeight()/2), currentColor);
		
		}
		{
			// Bottom border
			int x_rat = (getWidth() - (m_images[1].getWidth()/2) - (m_images[2].getWidth()/2)) / m_images[6].getWidth();
			int x_plus = (getWidth() - (m_images[1].getWidth()/2) - (m_images[2].getWidth()/2)) % m_images[6].getWidth();
			for (int i = 0; i < x_rat; i++)
				g.drawImage(m_images[6], 
					offsetX + getX() + i*m_images[6].getWidth() + (m_images[1].getWidth()/2),
					offsetY + getY() + getHeight() - (m_images[6].getHeight()/2), currentColor);
			if (x_plus > 0)
				g.drawImage(m_images[6].getSubImage(0, 0, x_plus, m_images[6].getHeight()),
					offsetX + getX() + x_rat*m_images[6].getWidth() + (m_images[1].getWidth()/2),
					offsetY + getY() + getHeight() - (m_images[6].getHeight()/2), currentColor);
		
		}
		{
			// Left border
			int y_rat = (getHeight() - (m_images[0].getHeight()/2) - (m_images[1].getHeight()/2)) / m_images[5].getHeight();
			int y_plus = (getHeight() - (m_images[0].getHeight()/2) - (m_images[1].getHeight()/2)) % m_images[5].getHeight();
			for (int i = 0; i < y_rat; i++)
				g.drawImage(m_images[5], 
					offsetX + getX() - (m_images[5].getWidth()/2),
					offsetY + getY() + i*m_images[5].getHeight() + (m_images[0].getHeight()/2), currentColor);
			if (y_plus > 0)
				g.drawImage(m_images[5].getSubImage(0, 0, m_images[5].getWidth(), y_plus),
					offsetX + getX() - (m_images[5].getWidth()/2),
					offsetY + getY() + y_rat*m_images[5].getHeight() + (m_images[0].getHeight()/2), currentColor);
		
		}

		{
			// Right border
			int y_rat = (getHeight() - (m_images[2].getHeight()/2) - (m_images[3].getHeight()/2)) / m_images[7].getHeight();
			int y_plus = (getHeight() - (m_images[2].getHeight()/2) - (m_images[3].getHeight()/2)) % m_images[7].getHeight();
			for (int i = 0; i < y_rat; i++)
				g.drawImage(m_images[7], 
					offsetX + getX() + getWidth() - (m_images[7].getWidth()/2),
					offsetY + getY() + i*m_images[7].getHeight() + (m_images[3].getHeight()/2), currentColor);
			if (y_plus > 0)
				g.drawImage(m_images[7].getSubImage(0, 0, m_images[7].getWidth(), y_plus),
					offsetX + getX() + getWidth() - (m_images[7].getWidth()/2),
					offsetY + getY() + y_rat*m_images[7].getHeight() + (m_images[3].getHeight()/2), currentColor);
		
		}

		// Draw upper left corner
		g.drawImage(m_images[0], offsetX + getX() - (m_images[0].getWidth()/2), 
			offsetY + getY() - (m_images[0].getHeight()/2), currentColor);
		
		// Draw bottom left corner
		g.drawImage(m_images[1], offsetX + getX() - (m_images[1].getWidth()/2),
			offsetY + getHeight() + getY() - (m_images[1].getHeight()/2), currentColor);
		
		// Draw bottom right corner
		g.drawImage(m_images[2], offsetX + getWidth() + getX() - (m_images[2].getWidth()/2),
			offsetY + getHeight() + getY() - (m_images[2].getHeight()/2), currentColor);
		
		// Draw upper right corner
		g.drawImage(m_images[3], offsetX + getWidth() + getX() - (m_images[3].getWidth()/2),
			offsetY + getY() - (m_images[3].getHeight()/2), currentColor);
		
	}

}
